package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.ruicar.afs.cloud.afscase.mq.entity.CommonFailMqInfo;
import com.ruicar.afs.cloud.afscase.mq.service.CommonFailMqInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.YesOrNoEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.common.SendMqFailedDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * <p>Description：mq发送失败保存信息</p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @createDate 2020-10-13
 */
@AllArgsConstructor
@Slf4j
@Component
public class SaveFailedMessageProcessor implements AfsMqBizProcessor<SendMqFailedDto> {

    private final CommonFailMqInfoService commonFailMqInfoService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(SendMqFailedDto entity) throws Exception {
        this.saveMessage(entity);
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_COMMON_COMMON_CASE_CTM_SAVE_FAILED_MESSAGE;
    }

    private void saveMessage(SendMqFailedDto entity){
        CommonFailMqInfo info = new CommonFailMqInfo();
        BeanUtils.copyProperties(entity,info);
        info.setStatus(YesOrNoEnum.no);
        info.setLastFailedTime(Calendar.getInstance().getTime());
        commonFailMqInfoService.save(info);
    }

}
